// import { App } from "./app"
// import {BulkLoader} from './utils/BulkLoader';
// import {ImageLoader} from './utils/ImageLoader';
///<reference path='./app.ts'/>
///<reference path='./utils/BulkLoader.ts'/>
///<reference path='./utils/ImageLoader.ts'/>

class UILayer extends lg.Container {
    private _progressBg: lg.Bitmap = null;
    private _progressBar: lg.Bitmap = null;
    private _btnmusic: lg.Bitmap = null;
    private _btnBack: lg.Bitmap = null;
    private _ufoStrings:Array<string>;
    constructor() {
        super();
        this._ufoStrings=new Array<string>();
        var ufosArr = window["ufosArr"];
        for (var i = 0; i < ufosArr.length; i++) {
            var ufo = ufosArr[i].name;
            this._ufoStrings.push(ufo);
                
        }
        // this._ufoStrings.push("meteorolite1");
        // this._ufoStrings.push("meteorolite2");
        // this._ufoStrings.push("meteorolite3");
        // this._ufoStrings.push("celestial-body1");
        // this._ufoStrings.push("celestial-body2");
        // this._ufoStrings.push("astronaut");
        // this._ufoStrings.push("satellite");
        // // this._ufoStrings.push("star");
        // this._ufoStrings.push("shooting-star");
        this.initSound();
        this.addEventListener("added", this.addedHandler);
    }
    private addedHandler(e) {
        e.target.removeEventListener("added", e.target.addedHandler);
        e.target.init();
        e.target.ufoUpdate();
    }
    private init(): void {
        this.y = 20;
        let canvas: HTMLCanvasElement = <HTMLCanvasElement>this.stage.canvas;
        this._progressBg = new lg.Bitmap(App.loadManager.getImage("progress_bg"));
        this._progressBg.x = canvas.width - this._progressBg.getBounds().width >> 1;
        this._progressBg.y = 52;
        this.stage.addChild(this._progressBg);

        this._progressBar = new lg.Bitmap(App.loadManager.getImage("progress_bar"));
        this._progressBar.x = canvas.width - this._progressBar.getBounds().width >> 1;
        // this._progressBar.x=(canvas.width-this._progressBar.getBounds().width>>1)-this._progressBar.getBounds().width*0.5;;
        this._progressBar.y = 54;

        this.stage.addChild(this._progressBar);

        this._btnBack = new lg.Bitmap(App.loadManager.getImage("btnback"));
        this._btnBack.x = 40;
        this._btnBack.y = 40;
        this._btnBack.addEventListener("click", () => { window["backBtnHandler"]() }, null);
        this.stage.addChild(this._btnBack);

        this._btnmusic = new lg.Bitmap(App.loadManager.getImage("btnmusic"));
        this._btnmusic.x = canvas.width - 40 - this._btnmusic.getBounds().width;
        this._btnmusic.y = 40;
        this._btnmusic.addEventListener("click", (e) => { this.musicHandler(e) }, null)
        this.stage.addChild(this._btnmusic);

        var time: lg.Text = new lg.Text("0分", "40px Arial", "#FF00FF");
        var totalTime: number = 180;
        var t: number = totalTime;
        time.text = t + "秒";
        time.textAlign = "center"
        time.x = canvas.width >> 1
        time.y = 10;
        this.stage.addChild(time);

        setInterval(() => {
            this._progressBar.scaleX = t / totalTime;
            t--;
            time.text = t + "秒";
            if (t == 0) {
                t = totalTime;
            }
        }, 1000);
        
    }
    private static musicPlaying: boolean = true;
    private musicHandler(e): void {
        // if (UILayer.musicPlaying) {
        //     UILayer.bgSound.stop();
        //     UILayer.eatSound.stop();
        //     UILayer.musicPlaying = false;
        // } else {
        //     UILayer.bgSound.play();
        //     UILayer.musicPlaying = true;
        // }
        window["musicHandler"]();
    }
    public static eatSoundPlay() {
        // if (UILayer.musicPlaying)
        //     UILayer.eatSound.play();
        window["arrivedMusic"]();
    }
    private initSound() {
        var sounds = [
            { src: "../res/assets/audios/bg.mp3", id: "bg" },
            { src: "../res/assets/audios/eat.mp3", id: "eat" }
        ];
        lg.Sound.alternateExtensions = ["mp3"];
        lg.Sound.on("fileload", this.loadHandler, this);
        lg.Sound.registerSounds(sounds);
        // alert("我去加载音乐")
        // lg.Sound.registerSound("bin/assets/audios/eat.mp3", "eat");

    }
    public static bgSound: lg.AbstractSoundInstance;
    public static eatSound: lg.AbstractSoundInstance;
    private loadHandler(event) {
        // alert("音乐加载好了")
        UILayer.bgSound = lg.Sound.play("bg");
        // instance.volume = 0.1;
        UILayer.bgSound.setLoop(-1);

        UILayer.eatSound = lg.Sound.createInstance("eat");
        //instance.volume = 0.1;
    }
    
    private ufoUpdate():void
    {
        let canvas: HTMLCanvasElement = <HTMLCanvasElement>this.stage.canvas;
        var random: number = Math.floor(Math.random() * this._ufoStrings.length);
        var name: string = this._ufoStrings[random];
        
        var b: lg.Bitmap = new lg.Bitmap(App.loadManager.getImage(name));
        this.addChild(b);
         var startPoint:lg.Point;
        var endPoint:lg.Point;
        if(Math.random()>0.5)
        {
            startPoint=new lg.Point(Math.random()*canvas.width+100,0);
            endPoint=new lg.Point(0-100, Math.random()* canvas.height+100);
        }
        else
        {
            startPoint=new lg.Point(0,0);
            endPoint=new lg.Point(canvas.width+100,Math.random()* canvas.height+100);
        }
       
        var duration:number=Math.random()*5;
        
        TweenMax.fromTo(b,duration,{x:startPoint.x,y:startPoint.y},{x:endPoint.x,y:endPoint.y, ease:Linear.easeNone, onComplete:()=>{
            // console.log(b);
            b.parent.removeChild(b);
        }
        });
        setTimeout(
        ()=>this.ufoUpdate(),Math.random()*6000
        
        );
    }

}